package com.lzdongrui.backend.service.impl;

import java.util.Date;
import java.util.List;

import com.lzdongrui.common.exception.ServiceException;
import com.lzdongrui.common.utils.DateUtils;
import com.lzdongrui.common.utils.snowid.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lzdongrui.backend.mapper.SemesterMapper;
import com.lzdongrui.backend.domain.Semester;
import com.lzdongrui.backend.service.ISemesterService;

/**
 * 学期Service业务层处理
 * 
 * @author hjs
 * @date 2023-05-04
 */
@Service
public class SemesterServiceImpl implements ISemesterService 
{
    @Autowired
    private SemesterMapper semesterMapper;

    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    /**
     * 查询学期
     * 
     * @param id 学期主键
     * @return 学期
     */
    @Override
    public Semester selectSemesterById(Long id)
    {
        return semesterMapper.selectSemesterById(id);
    }

    /**
     * 查询学期列表
     * 
     * @param semester 学期
     * @return 学期
     */
    @Override
    public List<Semester> selectSemesterList(Semester semester)
    {
        return semesterMapper.selectSemesterList(semester);
    }

    /**
     * 新增学期
     * 
     * @param semester 学期
     * @return 结果
     */
    @Override
    public int insertSemester(Semester semester)
    {
        semester.setId(snowflakeIdWorker.nextId());
        Date nowDate = DateUtils.getNowDate();
        if (!(nowDate.after(semester.getStartTime()) && nowDate.before(semester.getEndTime())) && semester.getCurrent() == 0) {
            throw new ServiceException("不在时间段内,不能设置为当前学期");
        }else {
            if (semester.getCurrent() == 0) {
                semesterMapper.updateAllCurrentNO();
            }
            semester.setCreateTime(DateUtils.getNowDate());
        }
        semester.setCreateTime(DateUtils.getNowDate());
        return semesterMapper.insertSemester(semester);
    }

    /**
     * 修改学期
     *
     * @param semester 学期
     * @return 结果
     */
    @Override
    public int updateSemester(Semester semester)
    {
        Semester semesterData = semesterMapper.selectSemesterById(semester.getId());
        Date nowDate = DateUtils.getNowDate();
        if (nowDate.after(semesterData.getStartTime()) && nowDate.before(semesterData.getEndTime()) && semester.getCurrent() == 1) {
            throw new ServiceException("在时间段内无法改变学期");
        }
        if (!(nowDate.after(semester.getStartTime()) && nowDate.before(semester.getEndTime())) && semester.getCurrent() == 0) {
            throw new ServiceException("不在时间段内,不能设置为当前学期");
        }
        else {
            if (semester.getCurrent() == 0) {
                semesterMapper.updateAllCurrentNO();
            }
            semester.setUpdateTime(DateUtils.getNowDate());
        }
        return semesterMapper.updateSemester(semester);
    }

    /**
     * 批量删除学期
     * 
     * @param ids 需要删除的学期主键
     * @return 结果
     */
    @Override
    public int deleteSemesterByIds(Long[] ids)
    {
        return semesterMapper.deleteSemesterByIds(ids);
    }

    /**
     * 删除学期信息
     * 
     * @param id 学期主键
     * @return 结果
     */
    @Override
    public int deleteSemesterById(Long id)
    {
        return semesterMapper.deleteSemesterById(id);
    }

    /**
     * 查询学年信息
     *
     * @return
     */
    @Override
    public List<Semester> selectschoolYearList() {
        return semesterMapper.selectschoolYearList();
    }
}
